<?php
namespace app\admin\controller;

class Patch extends Base
{
    //碎片分类列表
    public function lists(){
        $list = $this->db->query("select * from patch_cat order by id desc");
        foreach ($list as $k => $v) {
            $list[$k]['childlist'] = $this->db->query("select * from patch where cat_id=? order by sort desc, id desc",[$v['id']]);
        }
        return $this->fetch(["list"=>$list]);
    }

    //添加碎片数据
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'cat_id'=>'require',
                'title'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            $d['create_time'] = time();
            $rs = $this->db->insert("patch",$d);
            $d['pic'] = $d['content'];
            m('Attachment','xcom')->imgSure('patch',$rs,$d);
            if ($rs) {
                cookie('js_msg','添加成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('添加失败');
            }
        }
        $this->fetch(["cat_id"=>$this->get("cat_id")]);
    }

    //更新字段值
    public function upData(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'id'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            $data = [
                'id'=>$d['id'],
                $d['field']=>$d['value'],
                'update_time'=>time(),
            ];
            $this->db->update('patch',$data);
            $data['pic'] = $data['content'];
            m('Attachment','xcom')->imgSure('patch',$d['id'],$data);
            suc();
        }
    }

    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $this->db->delete('patch',$d['id']);
            return suc();
        }
    }
}
